home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Graphics / sKulpt / skulpt-src / Ami-SafeClose.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-26  |  1.7 KB  |  60 lines

  1. #include "windows.h"
  2.  
  3. /*
  4. ** function to remove and reply all IntuiMessages on a port that have been
  5. ** sent to a particular hWnddow (note that we don't rely on the ln_Succ
  6. ** pointer of a message after we have replied it)
  7. */
  8. VOID StripIntuiMessages(struct MsgPort *hMsgPort, HWND hWnd)
  9. {
  10.     struct IntuiMessage *hMsg = (struct IntuiMessage *) hMsgPort->mp_MsgList.lh_Head;
  11.     struct Node *hNext;
  12.  
  13.     while (hMsg && (hNext = hMsg->ExecMessage.mn_Node.ln_Succ))
  14.       {
  15.           if (hMsg -> IDCMPWindow == hWnd)
  16.           {
  17.             /* Intuition is about to free this message.
  18.             ** Make sure that we have politely sent it back.
  19.             */
  20.             Remove((struct Node *) hMsg);
  21.  
  22.             ReplyMsg((struct Message *) hMsg);
  23.           }
  24.           hMsg = (struct IntuiMessage *)hNext;
  25.       }
  26. }
  27.  
  28. /*
  29. ** Entry point to CloseWindowSafely()
  30. ** Strip all IntuiMessages from an IDCMP which are waiting for a specific
  31. ** hWnddow.  When the messages are gone, set the UserPort of the hWnddow to
  32. ** NULL and call ModifyIDCMP(hWnd,0).  This will free the Intuition arts of
  33. ** the IDMCMP and trun off message to this port without changing the
  34. ** original UserPort (which may be in use by other hWnddows).
  35. */
  36. VOID CloseWindowSafely(HWND hWnd)
  37. {
  38.     if (!hWnd) return;
  39.  
  40.     /* we forbid here to keep out of race conditions with Intuition */
  41.     Forbid();
  42.  
  43.     /* send back any messages for this hWnddow  that have not yet been
  44.     ** processed
  45.     */
  46.     StripIntuiMessages(hWnd->UserPort, hWnd);
  47.  
  48.     /* clear UserPort so Intuition will not free it */
  49.     hWnd->UserPort = NULL;
  50.  
  51.     /* tell Intuition to stop sending more messages */
  52.     ModifyIDCMP(hWnd, 0L);
  53.  
  54.     /* turn multitasking back on */
  55.     Permit();
  56.  
  57.     /* Now it's safe to really close the hWnddow */
  58.     CloseWindow(hWnd);
  59. }
  60.